using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;

namespace Windy.Transformations
{
    public static class TransformRuleExtension
    {
        public static TOutput TransformAsMono<TKey, TInput, TOutput>(this ITransformRule<TKey> rule, TKey key, TInput input)
        {
            Contract.Requires<ArgumentNullException>(rule != null, "rule is null.");

            Contract.Assume(rule is IMonoTransformRule<TKey, TInput, TOutput>, "rule must be ISingleOutputLRule<TKey,TItem>");

            var singleOutputRule = rule as IMonoTransformRule<TKey, TInput, TOutput>;

            return singleOutputRule.GenerateOutput(key, input);
        }

        public static IEnumerable<TOutput> TransformAsVary<TKey, TInput, TOutput>(this ITransformRule<TKey> rule, TKey key, TInput input)
        {
            Contract.Requires<ArgumentNullException>(rule != null, "rule is null.");

            Contract.Assume(rule is IVaryTransformRule<TKey, TInput, TOutput>, "rule must be IVariantOutputRule<TKey,TItem>");

            var variantOutputRule = rule as IVaryTransformRule<TKey, TInput, TOutput>;

            return variantOutputRule.GenerateOutput(key, input);
        }

        public static bool CheckRuleDataType<TKey, TInput, TOutput>(this ITransformRule<TKey> rule)
        {
            Contract.Requires<ArgumentNullException>(rule != null, "rule is null.");

            switch (rule.RuleType)
            {
                case TransformRuleTypes.VoidTransformRule:
                    return true;
                case TransformRuleTypes.MonoTransformRule:
                case TransformRuleTypes.VaryTransformRule:
                    return rule is ITransformRule<TKey, TInput, TOutput>;
                default:
                    throw new Exception("Unexpected RuleType");
            }
        }
    }
}
